package com.amazon.shop.controller;


import cn.hutool.core.bean.BeanUtil;
import com.amazon.shop.bean.dto.BaseGroup;
import com.amazon.shop.bean.dto.PriductForm;
import com.amazon.shop.bean.entity.Priduct;
import com.amazon.shop.bean.enums.ResultEnum;
import com.amazon.shop.bean.vo.PageVO;
import com.amazon.shop.bean.vo.Ret;
import com.amazon.shop.bean.vo.TblPriductVO;
import com.amazon.shop.cache.LocalCache;
import com.amazon.shop.service.IUserService;
import com.amazon.shop.service.IPriductService;
import com.amazon.shop.service.IStoreService;
import com.amazon.shop.util.ResultUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzj
 * @since 2021-01-25
 */
@RestController
@RequestMapping("/priduct")
public class PriductController {

    @Autowired
    private IPriductService priductService;

    @Autowired
    private IStoreService storeService;

    @Autowired
    private IUserService userServuce;

    @RequestMapping("/get")
    public Ret getPriduct(@Validated(value = BaseGroup.get.class) PriductForm priductForm){
        Page<Priduct> page = new Page(priductForm.getCurrentNum(), priductForm.getPageSize());
        page.setOrders(OrderItem.descs("createtime"));
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("id","purl","pname","rcode","storeid","createtime","updatetime","pmsg","pnmsg");
        String operatorUserid = priductForm.getOperatorUserid();
        Integer ismanager = priductForm.getOismanager();
        List<TblPriductVO> result = new ArrayList<>();

        if (ismanager!=null&&ismanager==0&&priductForm.getStoreid()==null) {
            List<Integer> storeids = userServuce.listStoreidByUserid(operatorUserid);
            if (storeids!=null&&storeids.size() > 0) {
                queryWrapper.in("storeid",storeids);
            }else {
                return ResultUtil.success(new PageVO<>(page));
            }
        }
        if(!StringUtils.isEmpty(priductForm.getRcode())){
            queryWrapper.eq("rcode", priductForm.getRcode());
        }
        if(priductForm.getStoreid()!=null){
            queryWrapper.eq("storeid", priductForm.getStoreid());
        }
        if(priductForm.getStime()!=null&& priductForm.getEtime()!=null){
            queryWrapper.ge("createtime",new Date(priductForm.getStime()));
            queryWrapper.lt("createtime",new Date(priductForm.getEtime()));
        }
        page = priductService.page(page,queryWrapper);
        PageVO<TblPriductVO> tblEvalformVOPageVO = new PageVO<>(page);
        List<Priduct> records = page.getRecords();
        for (Priduct record : records) {
            TblPriductVO tblPriductVO = new TblPriductVO();
            BeanUtil.copyProperties(record,tblPriductVO);
            String storeName = (String) LocalCache.storenameCache.get(record.getStoreid());
            tblPriductVO.setStorename(storeName);
            result.add(tblPriductVO);
        }
        tblEvalformVOPageVO.setRecords(result);
        return ResultUtil.success(tblEvalformVOPageVO);
    }

    @RequestMapping("/outGet")
    public Ret outGetPriduct(@Validated(value = BaseGroup.get.class) PriductForm priductForm){
        Page<Priduct> page = new Page(priductForm.getCurrentNum(), priductForm.getPageSize());
        page.setOrders(OrderItem.descs("createtime"));
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("purl","pname","rcode","storeid","pmsg","pnmsg");
        if(!StringUtils.isEmpty(priductForm.getRcode())){
            queryWrapper.eq("rcode", priductForm.getRcode());
        }
        page = priductService.page(page,queryWrapper);
        PageVO<TblPriductVO> tblEvalformVOPageVO = new PageVO<>(page);
        return ResultUtil.success(tblEvalformVOPageVO);
    }

    @RequestMapping("/add")
    public Ret addPriduct(@Validated(value = BaseGroup.add.class) PriductForm priductForm){
        QueryWrapper queryWrapper = new QueryWrapper();
        Integer storeid = priductForm.getStoreid();
        queryWrapper.eq("id",storeid);
        int count1 = storeService.count(queryWrapper);
        if(count1 <= 0){
            return ResultUtil.error(ResultEnum.STOREEERROR);
        }
        String rcode = priductForm.getRcode();
        queryWrapper.clear();
        queryWrapper.eq("rcode",rcode);
        int count = priductService.count(queryWrapper);
        if(count > 0){
            return ResultUtil.error(ResultEnum.EXISTERROR.getCode(),ResultEnum.EXISTERROR.getServecode(),"rcode已存在");
        }
        Date date = new Date(System.currentTimeMillis());
        Priduct priduct = new Priduct();
        BeanUtil.copyProperties(priductForm, priduct);
        priduct.setCreatetime(date);
        priduct.setUpdatetime(date);
        priductService.save(priduct);
        return ResultUtil.success();
    }
    @RequestMapping("/update")
    public Ret updatePriduct(@Validated(value = BaseGroup.update.class) PriductForm priductForm){
        QueryWrapper queryWrapper = new QueryWrapper();

        Integer storeid = priductForm.getStoreid();
        if(storeid!=null){
            queryWrapper.eq("id",storeid);
            int count1 = storeService.count(queryWrapper);
            if(count1 <= 0){
                return ResultUtil.error(ResultEnum.STOREEERROR);
            }
        }
        String rcode = priductForm.getRcode();
        if(rcode!=null){
            queryWrapper.clear();
            queryWrapper.eq("rcode",rcode);
            queryWrapper.ne("id", priductForm.getId());
            int count = priductService.count(queryWrapper);
            if(count>0){
                return ResultUtil.error(ResultEnum.EXISTERROR.getCode(),ResultEnum.EXISTERROR.getServecode(),"rcode已存在");
            }
        }
        Date date = new Date(System.currentTimeMillis());
        Priduct priduct = new Priduct();
        BeanUtil.copyProperties(priductForm, priduct);
        priduct.setUpdatetime(date);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("id", priduct.getId());
        if (!StringUtils.isEmpty(priductForm.getPurl())) {
            wrapper.set("purl", priductForm.getPurl());
        }
        if (!StringUtils.isEmpty(priductForm.getPurl())) {
            wrapper.set("pname", priductForm.getPname());
        }
        if (!StringUtils.isEmpty(priductForm.getPurl())) {
            wrapper.set("rcode", priductForm.getRcode());
        }
        if (!StringUtils.isEmpty(priductForm.getPmsg())) {
            wrapper.set("pmsg", priductForm.getPmsg());
        }
        if (!StringUtils.isEmpty(priductForm.getPnmsg())) {
            wrapper.set("pnmsg", priductForm.getPnmsg());
        }
        priductService.update(priduct,wrapper);
        return ResultUtil.success();
    }
    @RequestMapping("/delete")
    public Ret deletePriduct(@Validated(value = BaseGroup.del.class) PriductForm priductForm){
        priductService.removeById(priductForm.getId());
        return ResultUtil.success();
    }


}
